package ustc.xiaoyang.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import ustc.xiaoyang.mapper.EmpExprMapper;
import ustc.xiaoyang.mapper.EmpMapper;
import ustc.xiaoyang.pojo.Emp;
import ustc.xiaoyang.pojo.EmpExpr;
import ustc.xiaoyang.pojo.EmpLog;
import ustc.xiaoyang.pojo.PageResult;
import ustc.xiaoyang.pojo.dto.EmpPageQueryDTO;
import ustc.xiaoyang.pojo.dto.LoginDTO;
import ustc.xiaoyang.service.EmpLogService;
import ustc.xiaoyang.service.EmpService;
import ustc.xiaoyang.utils.JwtUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = {Exception.class},propagation = Propagation.REQUIRES_NEW)
public class EmpServiceImpl implements EmpService {
    private final EmpMapper empMapper;
    private final EmpExprMapper empExprMapper;
    private final EmpLogService empLogService;
    public EmpServiceImpl(EmpMapper empMapper, EmpExprMapper empExprMapper, EmpLogService empLogService) {
        this.empMapper = empMapper;
        this.empExprMapper = empExprMapper;
        this.empLogService = empLogService;
    }

    @Override
    public PageResult<Emp> pageList(EmpPageQueryDTO empPageQueryDTO) {
        PageHelper.startPage(empPageQueryDTO.getPage(), empPageQueryDTO.getPageSize());
        List<Emp> empList = empMapper.pageList(empPageQueryDTO);
        Page<Emp> p = (Page<Emp>) empList;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public void add(Emp emp) {
        try {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            //添加员工
            empMapper.add(emp);
            Integer id = emp.getId();
            //批量添加员工工作经历
            if(!emp.getExprList().isEmpty()){
                emp.getExprList().forEach(expr -> expr.setEmpId(id));
                empExprMapper.addBatch(emp.getExprList());
            }
        } finally {
            empLogService.add(new EmpLog(null,LocalDateTime.now(),"添加员工" + emp.toString()));
        }

    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        empMapper.deleteByIds(ids);
        empExprMapper.deleteByEmpIds(ids);
    }

    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        if(!exprList.isEmpty()){
            exprList.forEach(expr -> expr.setEmpId(emp.getId()));
            empExprMapper.addBatch(exprList);
        }
    }

    @Override
    public List<Emp> list() {
        return empMapper.list();
    }

    @Override
    public LoginDTO login(Emp emp) {
        LoginDTO loginDTO = empMapper.getByUsernameAndPassword(emp);
        if(loginDTO != null){
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",loginDTO.getId());
            claims.put("username",loginDTO.getUsername());
            String jwt = JwtUtils.generateJwt(claims);
            loginDTO.setToken(jwt);
        }
        return loginDTO;
    }
}
